﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using Microhard.WeixinMP.Models;
using Microhard.WeixinMP.DataAccess;
using Microhard.Core;
using System.Web;

namespace Microhard.WeixinMP.DTO
{
    public static class WXModelMapperExtension
    {
        #region Conversation
        public static ConversationModel ToModel(this Conversation entity)
        {
            var model = Mapper.Map<ConversationModel>(entity);
            return model;
        }

        public static Conversation ToEntity(this ConversationModel model)
        {
            return Mapper.Map<Conversation>(model);
        }
        #endregion

        #region Image
        public static ImageModel ToModel(this Image entity)
        {
            var model = Mapper.Map<ImageModel>(entity);
            return model;
        }

        public static Image ToEntity(this ImageModel model)
        {
            return Mapper.Map<Image>(model);
        }
        #endregion

        #region Account
        public static WXMPAccountModel ToModel(this MPAccount entity)
        {
            var model = Mapper.Map<WXMPAccountModel>(entity);
            return model;
        }

        public static MPAccount ToEntity(this WXMPAccountModel model)
        {
            return Mapper.Map<MPAccount>(model);
        }
        #endregion

        #region MPUsers
        public static WXMPUserModel ToModel(this MPUser entity)
        {
            WXMPUserModel model = new WXMPUserModel();
            try
            {
                model = Mapper.Map<WXMPUserModel>(entity);
            }
            catch (Exception ex)
            {
                Log.Error("WXMPUserModel.ToModel", "获得微信用户数据失败:" + ex);
            }
            model.Account = new WXMPAccountModel
            {
                UserName = entity.MPAccount.UserName,
                Password = entity.MPAccount.Password,
                CreateTime = entity.MPAccount.CreateTime.Value,
                UpdateTime = entity.MPAccount.UpdateTime.Value,
                WXMPID = entity.MPAccount.WXMPID
            };
            return model;
        }

        public static MPUser ToEntity(this WXMPUserModel model)
        {
            return Mapper.Map<MPUser>(model);
        }
        #endregion

        #region Info
        public static SendToUserInfoModel ToModel(this MPInfo entity)
        {
            var model = Mapper.Map<SendToUserInfoModel>(entity);
            model.Content = HttpUtility.HtmlDecode(model.Content);
            return model;
        }

        public static MPInfo ToEntity(this SendToUserInfoModel model)
        {
            return Mapper.Map<MPInfo>(model);
        }
        #endregion

        #region Feedback
        public static FeedbackModel ToModel(this Feedback entity)
        {
            var model = Mapper.Map<FeedbackModel>(entity);
            return model;
        }

        public static Feedback ToEntity(this FeedbackModel model)
        {
            return Mapper.Map<Feedback>(model);
        }
        #endregion

        #region Article
        public static ArtileModel ToModel(this Article entity)
        {
            var model = Mapper.Map<ArtileModel>(entity);
            model.ArticleContent = HttpUtility.HtmlDecode(model.ArticleContent);
            return model;
        }

        public static Article ToEntity(this ArtileModel model)
        {
            return Mapper.Map<Article>(model);
        }
        #endregion

        #region ArticleLink
        public static ShareLinkModel ToModel(this ShareLink entity)
        {
            var model = Mapper.Map<ShareLinkModel>(entity);
            return model;
        }

        public static ShareLink ToEntity(this ShareLinkModel model)
        {
            return Mapper.Map<ShareLink>(model);
        }
        #endregion

    }
}
